package com.mz.ozy.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.activity.shop.CarActivity;
import com.mz.ozy.adapter.MsgAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.MsgBean;
import com.mz.ozy.bean.MsgDetailBean;
import com.mz.ozy.bean.OrderMsgBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MsgPresenter;
import com.mz.ozy.mvp.view.MsgContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.sobot.chat.ZCSobotApi;
import com.sobot.chat.api.model.Information;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MsgActivity extends BaseActivity<MsgContract.View, MsgPresenter> implements MsgContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.ll_online_service)
    LinearLayout mLlOnlineService;
    @BindView(R.id.order_msg_service)
    LinearLayout mOrderMsgService;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.image_ordermsg)
    ImageView mImageOrdermsg;
    private MsgAdapter mAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<MsgBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private MeBean mMeBean;
    private static Activity sPreActivity;

    public static void go(Activity activity) {
        sPreActivity = activity;
        activity.startActivity(new Intent(activity, MsgActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText("消息");
        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MsgAdapter(this, mBeanList);
        mRecyclerview.setAdapter(mAdapter);
        mRefreshLayout.setEnableAutoLoadmore(true);
        mRefreshLayout.autoRefresh();
        mPresenter.getMeInfo(this, false);
        //mPresenter.getUnreadMsg(this);
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getMsgList(MsgActivity.this, "1", String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getMsgList(MsgActivity.this, "1", String.valueOf(pageNo));

            }
        });
        mLlOnlineService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startActivity(new Intent(MsgActivity.this, CustomerServiceActivity.class));
                Information info = new Information();
                info.setApp_key("f970b1112b8447b6abe76c7acbf450cd");
                info.setPartnerid(Constants.uid);
                info.setUser_nick(mMeBean.getNickname());
                info.setUser_name(mMeBean.getUsername());
                info.setUser_tels(mMeBean.getUsername());
                info.setFace(mMeBean.getUseravatar());
                ZCSobotApi.openZCChat(MsgActivity.this, info);
            }
        });
        mOrderMsgService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MsgActivity","点击了订单消息");
                startActivity(new Intent(MsgActivity.this, MsgOrderActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getUnreadMsg(this);
    }

    @Override
    public void getMsgrListSuccess(MsgBean bean) {
        if (tag == 0) {
            mRefreshLayout.setLoadmoreFinished(false);
            mRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            mRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getMsgDetailSuccess(MsgDetailBean jsonObject) {

    }
    @Override
    public void getOrderMsgrListSuccess(OrderMsgBean bean) {

    }

    @Override
    public void getUnreadMsgSuccess(UnreadMsgBean bean) {
        if(bean.getNums()>0) {
            mImageOrdermsg.setVisibility(View.VISIBLE);
        }
        else
        {
            mImageOrdermsg.setVisibility(View.GONE);
        }
    }

    @Override
    public void getAgmentDetailSuccess(JSONObject jsonObject) {

    }

    @Override
    public void getgetMeInfoSuccess(MeBean bean) {
        mMeBean = bean;
    }
}
